package featureautomate.popup.actions;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.xtext.ui.editor.copyqualifiedname.ClipboardUtil;

import run.LtsRunner;
import featureautomate.structure.Automate;
import featureautomate.tools.AdapterAutomate;
import featureautomate.tools.Tools;

public class DotFileGeneration implements IObjectActionDelegate {

	private Shell shell;
	
	private ISelection selection;
	
	
	/**
	 * Constructor for Action1.
	 */
	public DotFileGeneration() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		
		IFile file = getFile();
	    IPath osPath = file.getLocation();
	    //ClipboardUtil.copy(osPath.toFile().getAbsolutePath());
	    
	    if(osPath.toFile().getAbsolutePath().contains(".lts")){
	    	Automate a = AdapterAutomate.adapterStatemachine(
					LtsRunner.ParseLtsProgram(osPath.toFile().getAbsolutePath()));
	    
	    	Tools.generateDot(a, osPath.toFile().getAbsolutePath().replace(".lts", ".dot"));
	    	
	    	MessageDialog.openInformation(
	    			shell,
	    			"FeatureAutomate",
	    			"Generate DOT was executed to "+file.getName()+" .");
	    }
	    
		
		
		
	}
	
	
	/**
	   * @see IActionDelegate#selectionChanged(
	   *          IAction, ISelection)
	   */
	  public void selectionChanged(
	      IAction action, ISelection selection)
	  {
	    this.selection = selection;
	  }

	//IStructuredSelection
	  private IFile getFile() {
	    return (IFile)
	        ((IStructuredSelection) selection).getFirstElement();
	  }
}
